﻿using PPP.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PPP.Services {
    /// <summary>
    /// Picture Service to load  pictures from hard drive (e.g HDD, USB)
    /// 
    /// <list type="bullet">
    /// <item>
    /// <term>Author</term>
    /// <description>Sebastian Will</description>
    /// </item>
    /// </list>
    /// 
    /// </summary>  
    class HardDrivePicturesService : IPicturesService {

        #region Fields

        /// <summary>
        /// The root picture path.
        /// </summary>
        string _rootPath = string.Empty;

        /// <summary>
        /// Include sub directories.
        /// </summary>
        bool _includeSubDirectories = false;

        #endregion

        #region Constructors

        /// <summary>
        /// Constructor.
        /// </summary>
        public HardDrivePicturesService(string rootPath, bool includeSubDirectories) {
            _rootPath = rootPath;
            _includeSubDirectories = includeSubDirectories;        
        }

        #endregion

        #region Methods

        /// <summary>
        /// Get pictures service method.
        /// </summary>
        /// <param name="callback"></param>
        public void GetPictures(Action<IList<Picture>, Exception> callback) {

            List<Picture> result = new List<Picture>();

            //DirectoryInfo dir = new DirectoryInfo(@"C:\Users\admin\Documents\Visual Studio 2013\Projects\PPP\PPP\Images\Samples");
            DirectoryInfo dir = new DirectoryInfo(@_rootPath);
            SearchOption searchoption = _includeSubDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;

            FileInfo[] files = dir.GetFiles("*.jpg", searchoption);

            foreach (FileInfo fileInfo in files) {
                result.Add(new Picture(fileInfo));
            }

            callback(result, null);
        }

        #endregion
    }
}
